<?php


namespace app\user\controller;
use lib\AliPayPc;
use think\Controller;
use think\facade\Cache;
use think\facade\Session;
class Cart extends Controller
{

    //ajax添加到购物车
    public function addcart(){
        $data=input('post.');
        $d=[
            'num'=>$data['num']
        ];
        $user=Session::get('user');
        if(empty($user)){
            echo 0;
        }else{
            Cache::hSet("user_id_".$user['id'],$data['id'],json_encode($d));    //把数据添加到购物车用到的redis缓存
            echo "1";
        }
        \sleep(1);
    }

      //取到购物车商品的基本信息
      public function getcart(){
        $user=Session::get('user');
        if(empty($user)){
            $this->error('请先登录');
        }
        $sum_price=0;
        $total_num=0;
        
        $data=Cache::hgetAll('user_id_'.$user['id']); //取得redis里面购物车的缓存信息
        foreach ($data as $k=>$v){                 //遍历键
            $v=json_decode($v);
            $a=db('goods')->field(['sale_price','thumb','goods_name'])->where('id',$k)->find();
            foreach ($v as $key=>$value){
                $arr[$k][$key]=$value;
                $arr[$k]['sale_price']=$a['sale_price'];
                $arr[$k]['thumb']=$a['thumb'];
                $arr[$k]['goods_name']=$a['goods_name'];
                $arr[$k]['id']=$k;
            }
            $arr[$k]['total_price']=$a['sale_price']*$arr[$k]['num'];
            $sum_price+=$arr[$k]['total_price'];
            $total_num=$total_num+$arr[$k]['num'];
            //dump($arr[$k]['num']);

        }

        if(empty($arr)){
            $arr=1;
        }
       // dump($total_num);

        $this->assign(['arr'=>$arr,'sum_price'=>$sum_price,'total_num'=>$total_num]);
        return view();

    }


    //修改redis中的数量
    public function editcart(){
        $data=input('post.');
        $user=Session::get('user');
        $d=[
            'num'=>$data['num']
        ];
        $user=Session::get('user');
        Cache::hSet("user_id_".$user['id'],$data['id'],json_encode($d));
        echo "success";
    }

    //删除购物车中的某一件商品
    public function delcart(){
        $sum_price=0;
        $total_num=0;
        $data=input('post.');
        $user=Session::get('user');
        Cache::hdel("user_id_".$user['id'],$data['id']);
        $data1=Cache::hgetAll('user_id_'.$user['id']);
        foreach ($data1 as $k=>$v){                 //遍历键
            $v=json_decode($v);
            $a=db('goods')->field(['sale_price'])->where('id',$k)->find();
            foreach ($v as $key=>$value){
                $arr[$k][$key]=$value;
                $arr[$k]['sale_price']=$a['sale_price'];
            }
            $arr[$k]['total_price']=$a['sale_price']*$arr[$k]['num'];
            $sum_price+=$arr[$k]['total_price'];
            $total_num=$total_num+$arr[$k]['num'];
        }
        echo json_encode(['sum_price'=>$sum_price,'total_num'=>$total_num]);
    }


  

    //清空购物车
    public function cleanall(){
        $user=Session::get('user');
        $data=Cache::hgetAll('user_id_'.$user['id']);
        foreach ($data as $k=>$v){
            Cache::hDel("user_id_".$user['id'],$k);
        }
        echo "1";
    }




}